// 后台管理系统的通用请求 增删改查...

import { ref } from 'vue'

export const normalOptions = (options: any) => {
  const loading = ref(false) // 是否处于加载中
  const list = ref<any[]>([]) // 数据列表
  const optionDialogVisible = ref(false) // 是否显示弹窗
  const optionDoalogTitle = ref('') // 弹窗标题
  const currentId = ref(0) // 当前编辑的id
  const currentDetailId = ref<number>(0) // 当前编辑的详情的id
  const count = ref(0) // 数据总数
  const page = ref(1) // 当前页码
  const size = ref(5) // 每页显示条数

  // 获取数据
  const getList = async (p?: number, s?: number) => {
    page.value = p ? p : page.value
    size.value = s ? s : size.value
    loading.value = true
    const res = await options.getList(page.value, size.value)
    count.value = res.count ? res.count : 0
    loading.value = false
    list.value = res.rows ? res.rows : res
    return list
  }

  // 新增数据
  const addData = async (data?: any) => {
    loading.value = true
    let formData = {}
    if (data) {
      formData = data
    } else {
      formData = options.additionData()
    }
    // 调用接口
    options
      .addData(formData)
      .then((res: any) => {
        loading.value = false
        optionDialogVisible.value = false
        list.value.unshift(res)
      })
      .catch((err: any) => {
        loading.value = false
        optionDialogVisible.value = false
      })
  }

  // 修改数据
  const updateData = async (id: number, detail_id?: number) => {
    currentId.value = id
    detail_id ? (currentDetailId.value = detail_id as number) : ''
    optionDoalogTitle.value = '编辑'
    optionDialogVisible.value = true
    // 从列表中找到对应的数据
    const data = list.value.find((item: any) => item.id === id)
    options.assignmentData(data)
  }

  // 提交修改书数据
  const submitUpdateData = async (data?: any) => {
    let formData = {}
    loading.value = true
    if (data) {
      formData = data
    } else {
      formData = options.additionData(currentId.value, currentDetailId.value)
    }
    options
      .updateData(formData)
      .then((res: any) => {
        loading.value = false
        optionDialogVisible.value = false
        ElMessage.success('修改成功')
        console.log(res)
        // 更新列表
        getList(page.value, size.value)
      })
      .catch((err: any) => {
        loading.value = false
        optionDialogVisible.value = false
      })
  }

  // 删除数据
  const deleteData = async (id: number, detail_id?: number) => {
    console.log(id)
    loading.value = true
    options.deleteData(id, detail_id).then((res: any) => {
      loading.value = false
      list.value = list.value.filter((item: any) => item.id !== id)
      ElMessage.success('删除成功')
    })
  }

  // 文件上传
  const uploadFile = async (file: any) => {
    try {
      loading.value = true
      const formData = new FormData()
      formData.append('file', file)
      const res = await options.uploadFile(formData)
      loading.value = false
      return res
    } catch (error) {
      ElMessage.error('上传失败')
    }
  }

  // 页码发生改变
  const pageChangeHandler = async (e: number) => {
    loading.value = true
    await getList(e, size.value)
    loading.value = false
  }

  // 打开弹层
  const openDialog = () => {
    optionDoalogTitle.value = '新增'
    options.clearFormData()
    optionDialogVisible.value = true
  }

  // 关闭弹层
  const closeDialog = () => {
    optionDialogVisible.value = false
  }

  return {
    loading,
    list,
    optionDialogVisible,
    optionDoalogTitle,
    count,
    currentId,
    getList,
    openDialog,
    closeDialog,
    addData,
    deleteData,
    updateData,
    submitUpdateData,
    uploadFile,
    pageChangeHandler
  }
}
